package pip.can;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.border.TitledBorder;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.border.EtchedBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.SpinnerNumberModel;

import org.jgrapht.Graph;
import org.jgrapht.ListenableGraph;

import java.awt.Font;

public class MainWindow extends JFrame {

	private JPanel contentPane;
	private NetGraph ngraph;
	private JScrollPane panel_1;
	private JSpinner spinner_1;
	private JSpinner spinner_2;
	private JSpinner spinner_3;
	private JSpinner spinner_4;
	private JSpinner spinner_5;
	private JSpinner spinner_6;
	private JSpinner spinner_7;
	private JSpinner spinner;
	private int topologyIndex;
	/**
	 * Create the frame.
	 */
private void showDialogBox(){
		//DialogBox db = new DialogBox(this);
		ProgressDialog pr = new ProgressDialog(this);
}

private ArrayList<String> clientsList(){
	ListenableGraph g = ngraph.getG();
	Set verticles = g.vertexSet();
	Iterator iter = verticles.iterator();
	ArrayList<String> clients = new ArrayList<String>();
	boolean add;
	while(iter.hasNext()){
		add = true;
		Object tmp = iter.next();
		Set nodeEdges = g.edgesOf(tmp);
		for (Object edge : nodeEdges){
			if(g.getEdgeTarget(edge).equals(tmp)){
				add = false;
			}
		}
		if(add)
			clients.add(tmp.toString());
	}
	return clients;
}
private void startProgram(){
	StaticVariables.end = false;
	setVariables();
	ListenableGraph g = ngraph.getG();
	Path.setGraph(g);
//	Repository repo = new Repository();
//	for (int i =0 ; i<StaticVariables.Contents; i++){
//		repo.addContent(new Content(StaticVariables.contentSize,i));
//	}
//	StaticVariables.setRepository(repo);
	ProgressDialog pr = new ProgressDialog(this);
	StaticVariables.setResultBtn(pr.getResultBtn());
	StaticVariables.setProgressBar(pr.getProgressBar());
	Controller control = new Controller(createTopology(),clientsList());
	control.start();
	
	pr.setVisible(true);
}

public HashMap<String, Node> createTopology(){
		ListenableGraph g = ngraph.getG();
    	Set verticles = g.vertexSet();
    	Iterator iter = verticles.iterator();
    	HashMap<String, Node> topology = new HashMap<String, Node>();
    	while(iter.hasNext()){
    		Object tmp = iter.next();
    		Set nodeEdges = g.edgesOf(tmp);
    		Object forward = null;
    		for (Object edge : nodeEdges){
    			if(g.getEdgeSource(edge).equals(tmp)){
    				forward = g.getEdgeTarget(edge);
    				break;
    			}
    		}
    		Node node = new Node(tmp.toString(), (Integer)spinner.getValue(), forward == null? null : forward.toString());
    		topology.put(tmp.toString(), node);
    	}
  //  	for (Map.Entry<String, Node> entry : topology.entrySet() ) {
  //  	    String key = entry.getKey();
  //  	    Node value = entry.getValue();
   // 	    System.out.println("Node name: " + key.toString() + " Forward Node " + value.getForward());
   // 	}
   	
    	return topology;
}
    
private void createLinearNetwork(){
	ngraph.linearNetwork();
}
private void createTreeNetwork(){
	ngraph.treeNetwork();
}

private void setVariables(){
	StaticVariables.setChunkSize((Integer)spinner_1.getValue());
	StaticVariables.setRR((Integer)spinner_4.getValue());
	StaticVariables.setClasses((Integer)spinner_3.getValue());
	StaticVariables.setTopology(topologyIndex);
	StaticVariables.setlinkDelay((Integer)spinner_2.getValue());
	StaticVariables.setContents((Integer)spinner_5.getValue());
	StaticVariables.setContentSize((Integer)spinner_7.getValue());
	StaticVariables.setEndTime((Integer)spinner_6.getValue());
}
	public MainWindow() {
		ngraph = new NetGraph(0);
		setTitle("Content Aware Network Simulator");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 829, 606);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel.setBounds(12, 27, 212, 501);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblTopologiaSieci = new JLabel("Topologia sieci:");
		lblTopologiaSieci.setBounds(12, 12, 122, 15);
		panel.add(lblTopologiaSieci);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Liniowa", "Drzewo"}));
		comboBox.setBounds(12, 30, 172, 24);
		comboBox.addActionListener(
	             new ActionListener() {
	                 public void actionPerformed(ActionEvent e) {
	                	 JComboBox cb = (JComboBox)e.getSource();
	                     int item = cb.getSelectedIndex();
	                     if (item == 0){
	                    	 topologyIndex = item;
	                    	 ngraph.removeAll();
	                    	 createLinearNetwork();
	                     }else if(item == 1){
	                    	 topologyIndex = item;
	                    	 ngraph.removeAll();
	                    	 createTreeNetwork();
	                     }
	                 }
	             }
	         );
		panel.add(comboBox);
		
		JLabel lblCacheWza = new JLabel("Cache węzła:");
		lblCacheWza.setBounds(12, 58, 149, 15);
		panel.add(lblCacheWza);
		
		spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(new Integer(100), null, null, new Integer(1)));
		spinner.setBounds(12, 76, 96, 20);
		panel.add(spinner);
		
		JLabel lblWielkoChunku = new JLabel("Wielkość chunku (kb):");
		lblWielkoChunku.setBounds(12, 100, 172, 15);
		panel.add(lblWielkoChunku);
		
		spinner_1 = new JSpinner();
		spinner_1.setModel(new SpinnerNumberModel(new Integer(100), null, null, new Integer(1)));
		spinner_1.setBounds(12, 115, 96, 20);
		panel.add(spinner_1);
		
		JLabel lblOpnienieNaczach = new JLabel("Opóźnienie na łączach (us):");
		lblOpnienieNaczach.setBounds(12, 140, 200, 15);
		panel.add(lblOpnienieNaczach);
		
		spinner_2 = new JSpinner();
		spinner_2.setModel(new SpinnerNumberModel(new Integer(1), null, null, new Integer(1)));
		spinner_2.setBounds(12, 155, 96, 20);
		panel.add(spinner_2);
		
		JLabel lblLiczbaKlasRuchu = new JLabel("Liczba klas ruchu:");
		lblLiczbaKlasRuchu.setBounds(12, 180, 149, 15);
		panel.add(lblLiczbaKlasRuchu);
		
		spinner_3 = new JSpinner();
		spinner_3.setModel(new SpinnerNumberModel(new Integer(10), null, null, new Integer(1)));
		spinner_3.setBounds(12, 196, 96, 20);
		panel.add(spinner_3);
		
		JLabel lblMaxWielkoDanych = new JLabel("Wielkość danych DRR:");
		lblMaxWielkoDanych.setBounds(12, 220, 188, 15);
		panel.add(lblMaxWielkoDanych);
		
		spinner_4 = new JSpinner();
		spinner_4.setModel(new SpinnerNumberModel(new Integer(100), null, null, new Integer(1)));
		spinner_4.setBounds(12, 238, 96, 20);
		panel.add(spinner_4);
		
		JLabel lblLiczbaPlikw = new JLabel("Liczba plików:");
		lblLiczbaPlikw.setBounds(12, 263, 122, 15);
		panel.add(lblLiczbaPlikw);
		
		spinner_5 = new JSpinner();
		spinner_5.setModel(new SpinnerNumberModel(new Integer(100), null, null, new Integer(1)));
		spinner_5.setBounds(12, 280, 96, 20);
		panel.add(spinner_5);
		
		JLabel lblCzasSymulacji = new JLabel("Czas symulacji:");
		lblCzasSymulacji.setBounds(12, 306, 136, 15);
		panel.add(lblCzasSymulacji);
		
		spinner_6 = new JSpinner();
		spinner_6.setModel(new SpinnerNumberModel(new Integer(1000), null, null, new Integer(1)));
		spinner_6.setBounds(12, 323, 96, 20);
		panel.add(spinner_6);
		
		JLabel lblredniaWielkoPliku = new JLabel("Średnia wielkość pliku:");
		lblredniaWielkoPliku.setBounds(12, 352, 188, 15);
		panel.add(lblredniaWielkoPliku);
		
		spinner_7 = new JSpinner();
		spinner_7.setModel(new SpinnerNumberModel(new Integer(10), null, null, new Integer(1)));
		spinner_7.setBounds(12, 368, 96, 20);
		panel.add(spinner_7);
		
		JLabel lblredniaIloWywoa = new JLabel("Średnia ilość wywołań:");
		lblredniaIloWywoa.setBounds(12, 398, 172, 15);
		panel.add(lblredniaIloWywoa);
		
		JSpinner spinner_8 = new JSpinner();
		spinner_8.setModel(new SpinnerNumberModel(new Integer(1), null, null, new Integer(1)));
		spinner_8.setBounds(12, 415, 96, 20);
		panel.add(spinner_8);
		
		JLabel lblChunkw = new JLabel("chunków");
		lblChunkw.setFont(new Font("Ubuntu", Font.PLAIN, 12));
		lblChunkw.setBounds(115, 370, 70, 15);
		panel.add(lblChunkw);
		
		JLabel lblUstawienia = new JLabel("Ustawienia");
		lblUstawienia.setFont(new Font("Ubuntu", Font.BOLD, 16));
		lblUstawienia.setBounds(65, 12, 91, 15);
		contentPane.add(lblUstawienia);
		
		JButton btnStart = new JButton("START");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {	
				startProgram();
				
				
			}
		});
		btnStart.setBounds(698, 540, 117, 25);
		contentPane.add(btnStart);
		
		JButton btnKoniec = new JButton("KONIEC");
		btnKoniec.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnKoniec.setBounds(569, 540, 117, 25);
		contentPane.add(btnKoniec);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(236, 429, 579, 99);
		contentPane.add(scrollPane);
		
		JTextArea textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		
		
		
		panel_1 = new JScrollPane(ngraph.getGraph());
		panel_1.setBounds(236, 27, 579, 390);
		contentPane.add(panel_1);	
	}
}
